$(document).bind('mobileinit',function(){
});

var dt = dt || {};

dt.params = {
    tlds : ['com', 'net', 'org', 'eu', 'info']
};

dt.main = {
    
    retry : 0,
    
    items : {},
    
    init : function()
    {
        var params = {};
        
        dt.main.prefill();
        
        $('#form-search').bind('submit', function(e) {
            
          params.domain = $('#search-query').val();
          params.tlds = $('#field-tlds').val();

          dt.main.call(params, dt.main.renderList);

          return false;
        });

    },
    
    prefill : function() {
        
        if (!dt.util.empty(dt.params.tlds)) {
            $.each(dt.params.tlds, function(key, value) {

                $('#field-tlds option[value="' + value + '"]').attr('selected', 'selected');
            });
        }
        
        $('select.multi').selectmenu('refresh');
    },
    
    call : function(params, callback)
    {
        params = params || {};
        
        $.mobile.showPageLoadingMsg();
        
        var url = 'http://domains.pagodabox.com';
        
        $.each(dt.params, function(key, value) {
            if (value != null) params[key] = value;
        });

        console.log(params);
        
        $.ajax({
          url: url,
          data: params,
          success: function(data) {
            callback(data);
            $.mobile.hidePageLoadingMsg();
        },
          error : function() {
            
            if (dt.main.retry < 3) {
                
                dt.main.call(params, callback);
                dt.main.retry++;
            } else {
                dt.main.retry = 0;
            }
            
            $.mobile.hidePageLoadingMsg();
          },
          dataType: 'json'
        });
    },
    
    renderList : function(data, callback) {
    
      console.log(data);
       
      $('#result-list').empty();
      
      var items_list = '<li data-role="list-divider">Results for ' + data.domain + '</li>';
      
      dt.main.theme = '';

      $.each(data.message.data, function(key, val) {
          
        theme = val.status == 'free' ? 'data-theme="e"' : 'data-theme="b"';
        icon = val.status == 'free' ? 'data-icon="check"' : 'data-icon="delete"';
        
        console.log(theme);
        
        items_list += ''
        + '<li ' + theme + ' ' + icon + '><a class="link" rel="' + val.name + '" title="' + val.name + '">'
        + '<h3>' + val.name + '</h3>'
        +'<p>' + val.status + '</p></a><a href="">Details</a></li>';
      });
      
      $('#result-list').append(items_list).listview('refresh');
      
      if (typeof callback == 'function') callback();
      
      $('a.link').click(function(e) {
          dt.main.showDetails(this.rel);
      });

    },
    
    showDetails : function(domain)
    {
        var params = {type : 'info', domain : domain};
        
        dt.main.call(params, function(data) {
            console.log(data);
        });
    }
};

dt.settings = {
    init : function() {
        dt.settings.prefill();
    },
    
    prefill : function() {
        
        if (!dt.util.empty(dt.params.tlds)) {
            $.each(dt.params.tlds, function(key, value) {

                $('#field-tlds option[value="' + value + '"]').attr('selected', 'selected');
            });
        }
        
        $('select.multi').selectmenu('refresh');
    }
};

dt.util = {
    empty : function(obj) {
        return (typeof obj == "undefined") || obj === "" || obj === false || obj === null;
    }
};


$(document).ready(function(){
    dt.main.init();
    //$( '#settings' ).live( 'pageinit', dt.settings.init);
    //$( '#locality' ).live( 'pageinit', dt.geo.init);
});